package cz.data.domain.market.model;

import cz.data.common.exception.DataException;
import cz.data.domain.market.model.dto.TableApiSettings;
import org.apache.commons.lang3.StringUtils;

public class MarketUtil {
    private static final String prefix = "/api/tapi";

    public static String appendPrefixPath(String path) {
        return StringUtils.startsWith(path, "/") ? prefix + path : prefix + "/" + path;
    }

    public static String appendPrefixPath(String prefix, String path) {
        return StringUtils.startsWith(path, "/") ? prefix + path : prefix + "/" + path;
    }

    public static ApiResourceVo buildApiResource(String operate, String baseUrl) {
        String method;
        String url;
        if (TableApiSettings.Operate.Insert.equalsValue(operate)) {
            method = "POST";
            url = MarketUtil.appendPrefixPath(baseUrl);
        } else if (TableApiSettings.Operate.Update.equalsValue(operate)) {
            method = "PUT";
            url = MarketUtil.appendPrefixPath(baseUrl);
        } else if (TableApiSettings.Operate.Delete.equalsValue(operate)) {
            method = "DELETE";
            url = MarketUtil.appendPrefixPath(baseUrl + "/{id}");
        } else if (TableApiSettings.Operate.GetById.equalsValue(operate)) {
            method = "GET";
            url = MarketUtil.appendPrefixPath(baseUrl + "/{id}");
        } else if (TableApiSettings.Operate.Page.equalsValue(operate)) {
            method = "GET";
            url = MarketUtil.appendPrefixPath(baseUrl + "/page");
        } else {
            throw new DataException("错误的接口类型:" + operate);
        }
        return new ApiResourceVo(method, url);
    }
}
